package com.gomore.opple.module.cards.adapter;

import android.app.Activity;
import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.gomore.opple.R;
import com.gomore.opple.common.GlobalConstant;
import com.gomore.opple.rest.jdecard.GroupByOrderNumber;
import com.gomore.opple.utils.DateUtil;
import com.gomore.opple.utils.DensityUtil;
import com.gomore.opple.widgets.adapter.CommonAdapter;
import com.gomore.opple.widgets.adapter.ViewHolder;
import com.lsjwzh.widget.recyclerviewpager.RecyclerViewPager;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by asus on 2017/5/31.
 */
public class UsedCardsAdapter extends CommonAdapter<GroupByOrderNumber> {
    Activity mactivity;
    Context mContext;
    int width;
    int height;
    GroupByOrderNumber mgroupByOrderNumber;

    public UsedCardsAdapter(Activity activity, Context context, int layoutId, List<GroupByOrderNumber> messageList) {
        super(context, layoutId, messageList);
        mactivity = activity;
        mContext = context;
        width = DensityUtil.screenWith(context) - DensityUtil.dpToPx(mContext, 40);
        height = width * 290 / 620;
    }

    @Override
    protected void convert(ViewHolder holder, final GroupByOrderNumber groupByOrderNumber, final int position) {
        mgroupByOrderNumber = groupByOrderNumber;
//        final RecyclerView mRecyclerView = holder.getView(R.id.recyclerview_horizontal);
        final RecyclerViewPager mRecyclerView = holder.getView(R.id.recyclerview_horizontal);
        final TextView text = holder.getView(R.id.text);
        if (groupByOrderNumber.getECards().size() != 1) {
            text.setVisibility(View.VISIBLE);
            text.setText(1 + "/" + groupByOrderNumber.getECards().size());
        } else {
            text.setVisibility(View.GONE);
        }
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < groupByOrderNumber.getECards().size(); i++) {
            if (groupByOrderNumber.getECards().get(i).getDenomation() != null) {
                list.add(GlobalConstant.JDCard.getCardByMoney(groupByOrderNumber.getECards().get(i).getDenomation()));
            }
        }
        //横向RecyclerView
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mactivity, LinearLayoutManager.HORIZONTAL, false);
        mRecyclerView.setLayoutManager(linearLayoutManager);
        PagerAdapter pagerAdapter = new PagerAdapter(mactivity, mContext, R.layout.item_view_pager, groupByOrderNumber.getECards());
        mRecyclerView.setAdapter(pagerAdapter);
        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(width, height);
        layoutParams.setMargins(DensityUtil.dpToPx(mContext, 20), 0, 0, 0);
        mRecyclerView.setLayoutParams(layoutParams);
        mRecyclerView.setOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                int actualCurrentPosition = mRecyclerView.getCurrentPosition();
                text.setText((actualCurrentPosition + 1) + "/" + groupByOrderNumber.getECards().size());
            }
        });

        if (groupByOrderNumber.getOrderNumber() != null) {
            holder.setText(R.id.bill_number, groupByOrderNumber.getOrderNumber());
        }
        if (groupByOrderNumber.getOrderTime() != null) {
            holder.setText(R.id.time, DateUtil.fomatforDate(groupByOrderNumber.getOrderTime(), DateUtil.DATE_FORMATTER_1));
        }
    }
}
